/*     */ package com.java.diagnostics.visualizer.impl.sources.implementations;
/*     */ 
/*     */ import com.java.diagnostics.visualizer.exceptions.GCAndMemoryVisualizerException;
/*     */ import com.java.diagnostics.visualizer.impl.sources.SourceImpl;

/*     */ import java.io.BufferedReader;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
          import java.nio.charset.MalformedInputException;
/*     */ //import sun.io.MalformedInputException;
/*     */ 
/*     */ public class FileSourceImpl extends SourceImpl
/*     */ {
/*     */   protected File file;
/*     */   protected long lastModified;
/*     */ 
/*     */   public FileSourceImpl(File file)
/*     */   {
/*  31 */     this(file.getName(), file);
/*     */   }
/*     */ 
/*     */   public File getDirectory() {
/*  35 */     return this.file.getAbsoluteFile().getParentFile();
/*     */   }
/*     */ 
/*     */   public File getFile() {
/*  39 */     return this.file;
/*     */   }
/*     */ 
/*     */   public FileSourceImpl(String name, File file) {
/*  43 */     super(name);
/*  44 */     this.file = file;
/*  45 */     this.lastModified = -1L;
/*     */   }
/*     */ 
/*     */   public String getText() throws GCAndMemoryVisualizerException
/*     */   {
/*  50 */     this.lastModified = this.file.lastModified();
/*     */     try
/*     */     {
/*  53 */       StringBuffer buf = new StringBuffer();
/*  54 */       BufferedReader reader = getSafeBufferedStreamReader();
/*     */ 
/*  56 */       String txt = reader.readLine();
/*  57 */       while (txt != null) {
/*  58 */         buf.append(txt);
/*  59 */         buf.append(NEWLINE);
/*  60 */         txt = reader.readLine();
/*     */       }
/*     */ 
/*  64 */       return buf.toString();
/*     */     } catch (FileNotFoundException fe) {
/*  66 */       throw new GCAndMemoryVisualizerException(fe);
/*     */     } catch (IOException ioe) {
/*  68 */       throw new GCAndMemoryVisualizerException(ioe);
/*     */     }
/*     */   }
/*     */ 
/*     */   public boolean containsWithinReasonableDistanceOfStart(String target) throws GCAndMemoryVisualizerException
/*     */   {
/*  74 */     return containsWithinReasonableDistanceOfStart(new String[] { target });
/*     */   }
/*     */ 
/*     */   public boolean containsWithinReasonableDistanceOfStart(String[] targets) throws GCAndMemoryVisualizerException
/*     */   {
/*     */     try {
/*  80 */       boolean foundMatch = false;
/*     */ 
/*  82 */       BufferedReader reader = getSafeBufferedStreamReader();
/*  83 */       String line = reader.readLine();
/*  84 */       int lineCount = 0;
/*  85 */       while ((!foundMatch) && (line != null)) {
/*  86 */         for (int i = 0; i < targets.length; i++) {
/*  87 */           foundMatch = (foundMatch) || (line.indexOf(targets[i]) > -1);
/*     */         }
/*  89 */         line = reader.readLine();
/*  90 */         lineCount++;
/*     */       }
/*  92 */       this.lastModified = this.file.lastModified();
/*     */ 
/*  94 */       reader.close();
/*  95 */       return foundMatch;
/*     */     }
/*     */     catch (MalformedInputException localMalformedInputException)
/*     */     {
/*  99 */       return false;
/*     */     } catch (IOException ioe) {
/* 101 */       throw new GCAndMemoryVisualizerException(ioe);
/*     */     }
/*     */   }
/*     */ 
/*     */   public InputStream getRawStream() throws GCAndMemoryVisualizerException {
/* 106 */     this.lastModified = this.file.lastModified();
/*     */     try {
/* 108 */       return new FileInputStream(this.file);
/*     */     } catch (FileNotFoundException fe) {
/* 110 */       throw new GCAndMemoryVisualizerException(fe);
/*     */     }
/*     */   }
/*     */ 
/*     */   public boolean hasChangedSinceLastGet()
/*     */   {
/* 116 */     long fileLastModified = this.file.lastModified();
/* 117 */     boolean hasChanged = fileLastModified > this.lastModified;
/* 118 */     return hasChanged;
/*     */   }
/*     */ 
/*     */   public long getApproximateSize()
/*     */   {
/* 127 */     return this.file.length();
/*     */   }
/*     */ }

